{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras import layers\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "model = tf.keras.Sequential([\n",
    "# Adds a densely-connected layer with 64 units to the model:\n",
    "layers.Dense(64, activation='relu', input_shape=(32,)),\n",
    "# Add another:\n",
    "layers.Dense(64, activation='relu'),\n",
    "# Add a softmax layer with 10 output units:\n",
    "layers.Dense(10, activation='softmax')])\n",
    "\n",
    "model.compile(optimizer=tf.keras.optimizers.Adam(0.001),\n",
    "              loss='categorical_crossentropy',\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense (Dense)                (None, 64)                2112      \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 6,922\n",
      "Trainable params: 6,922\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "1000/1000 [==============================] - 0s 278us/sample - loss: 12.7523 - accuracy: 0.0890\n",
      "Epoch 2/10\n",
      "1000/1000 [==============================] - 0s 64us/sample - loss: 19.4926 - accuracy: 0.0930\n",
      "Epoch 3/10\n",
      "1000/1000 [==============================] - 0s 53us/sample - loss: 34.7675 - accuracy: 0.0950\n",
      "Epoch 4/10\n",
      "1000/1000 [==============================] - 0s 51us/sample - loss: 67.0130 - accuracy: 0.0910\n",
      "Epoch 5/10\n",
      "1000/1000 [==============================] - 0s 46us/sample - loss: 117.3077 - accuracy: 0.0990\n",
      "Epoch 6/10\n",
      "1000/1000 [==============================] - 0s 60us/sample - loss: 167.7246 - accuracy: 0.0980\n",
      "Epoch 7/10\n",
      "1000/1000 [==============================] - 0s 56us/sample - loss: 199.4483 - accuracy: 0.0930\n",
      "Epoch 8/10\n",
      "1000/1000 [==============================] - 0s 44us/sample - loss: 145.3299 - accuracy: 0.0830\n",
      "Epoch 9/10\n",
      "1000/1000 [==============================] - 0s 47us/sample - loss: 95.3213 - accuracy: 0.0940\n",
      "Epoch 10/10\n",
      "1000/1000 [==============================] - 0s 51us/sample - loss: 67.9918 - accuracy: 0.1040\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x133fdc668>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "data = np.random.random((1000, 32))\n",
    "labels = np.random.random((1000, 10))\n",
    "\n",
    "model.fit(data, labels, epochs=10, batch_size=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 1000 samples, validate on 100 samples\n",
      "Epoch 1/10\n",
      "1000/1000 [==============================] - 0s 200us/sample - loss: 38.4683 - accuracy: 0.0960 - val_loss: 20.3731 - val_accuracy: 0.0600\n",
      "Epoch 2/10\n",
      "1000/1000 [==============================] - 0s 61us/sample - loss: 23.0781 - accuracy: 0.1080 - val_loss: 20.9055 - val_accuracy: 0.1500\n",
      "Epoch 3/10\n",
      "1000/1000 [==============================] - 0s 68us/sample - loss: 32.9437 - accuracy: 0.1030 - val_loss: 55.5682 - val_accuracy: 0.1500\n",
      "Epoch 4/10\n",
      "1000/1000 [==============================] - 0s 67us/sample - loss: 79.3378 - accuracy: 0.1020 - val_loss: 70.4544 - val_accuracy: 0.1400\n",
      "Epoch 5/10\n",
      "1000/1000 [==============================] - 0s 65us/sample - loss: 72.6680 - accuracy: 0.1020 - val_loss: 133.7478 - val_accuracy: 0.0800\n",
      "Epoch 6/10\n",
      "1000/1000 [==============================] - 0s 68us/sample - loss: 84.5951 - accuracy: 0.1120 - val_loss: 57.5498 - val_accuracy: 0.0400\n",
      "Epoch 7/10\n",
      "1000/1000 [==============================] - 0s 89us/sample - loss: 99.2726 - accuracy: 0.1000 - val_loss: 161.7707 - val_accuracy: 0.0900\n",
      "Epoch 8/10\n",
      "1000/1000 [==============================] - 0s 77us/sample - loss: 109.4252 - accuracy: 0.0970 - val_loss: 130.7405 - val_accuracy: 0.0800\n",
      "Epoch 9/10\n",
      "1000/1000 [==============================] - 0s 82us/sample - loss: 117.2469 - accuracy: 0.0940 - val_loss: 190.9360 - val_accuracy: 0.1200\n",
      "Epoch 10/10\n",
      "1000/1000 [==============================] - 0s 92us/sample - loss: 191.2483 - accuracy: 0.0980 - val_loss: 313.3480 - val_accuracy: 0.0800\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x1345c8438>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "data = np.random.random((1000, 32))\n",
    "labels = np.random.random((1000, 10))\n",
    "\n",
    "val_data = np.random.random((100, 32))\n",
    "val_labels = np.random.random((100, 10))\n",
    "\n",
    "model.fit(data, labels, epochs=10, batch_size=32,\n",
    "          validation_data=(val_data, val_labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0524 10:22:29.064996 140734830638528 training_utils.py:1353] Expected a shuffled dataset but input dataset `x` is not shuffled. Please invoke `shuffle()` on input dataset.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "32/32 [==============================] - 0s 6ms/step - loss: 195.0060 - accuracy: 0.1050 - val_loss: 108.3701 - val_accuracy: 0.1600\n",
      "Epoch 2/10\n",
      "32/32 [==============================] - 0s 3ms/step - loss: 182.0231 - accuracy: 0.0850 - val_loss: 218.0477 - val_accuracy: 0.0700\n",
      "Epoch 3/10\n",
      "32/32 [==============================] - 0s 3ms/step - loss: 276.0313 - accuracy: 0.0870 - val_loss: 168.2823 - val_accuracy: 0.0700\n",
      "Epoch 4/10\n",
      "32/32 [==============================] - 0s 3ms/step - loss: 292.6820 - accuracy: 0.0900 - val_loss: 400.9864 - val_accuracy: 0.0700\n",
      "Epoch 5/10\n",
      "32/32 [==============================] - 0s 3ms/step - loss: 346.4063 - accuracy: 0.1060 - val_loss: 300.2910 - val_accuracy: 0.1600\n",
      "Epoch 6/10\n",
      "32/32 [==============================] - 0s 3ms/step - loss: 375.8087 - accuracy: 0.0840 - val_loss: 405.1963 - val_accuracy: 0.1200\n",
      "Epoch 7/10\n",
      "32/32 [==============================] - 0s 4ms/step - loss: 375.8030 - accuracy: 0.1010 - val_loss: 414.8658 - val_accuracy: 0.1600\n",
      "Epoch 8/10\n",
      "32/32 [==============================] - 0s 4ms/step - loss: 528.2368 - accuracy: 0.1000 - val_loss: 568.8161 - val_accuracy: 0.0900\n",
      "Epoch 9/10\n",
      "32/32 [==============================] - 0s 3ms/step - loss: 504.2863 - accuracy: 0.0950 - val_loss: 333.8356 - val_accuracy: 0.1600\n",
      "Epoch 10/10\n",
      "32/32 [==============================] - 0s 4ms/step - loss: 459.3776 - accuracy: 0.0980 - val_loss: 364.5391 - val_accuracy: 0.0400\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x133fc1ac8>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset = tf.data.Dataset.from_tensor_slices((data, labels))\n",
    "dataset = dataset.batch(32)\n",
    "\n",
    "val_dataset = tf.data.Dataset.from_tensor_slices((val_data, val_labels))\n",
    "val_dataset = val_dataset.batch(32)\n",
    "\n",
    "model.fit(dataset, epochs=10,\n",
    "          validation_data=val_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000/1000 [==============================] - 0s 77us/sample - loss: 347.5617 - accuracy: 0.0980\n",
      "30/30 [==============================] - 0s 2ms/step - loss: 355.5839 - accuracy: 0.0885\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[355.5838673909505, 0.088541664]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.random((1000, 32))\n",
    "labels = np.random.random((1000, 10))\n",
    "\n",
    "model.evaluate(data, labels, batch_size=32)\n",
    "\n",
    "model.evaluate(dataset, steps=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1000, 10)\n"
     ]
    }
   ],
   "source": [
    "result = model.predict(data, batch_size=32)\n",
    "print(result.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
